package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.http.client.ClientProtocolException;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.util.ConfigUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.util.BODY;
import com.util.HttpRequest;
import com.util.IpUtil;
import com.util.JSONUtil;
import com.util.MD5;
import com.util.OpenKey;
import com.util.ReadConfig;
import com.util.SessionInfo;

@Controller
@RequestMapping("/logincontroller")
public class logincontroller {
	private static final Logger logger = Logger.getLogger(dkjdcontroller.class);
	Map map = new HashMap();
	List list = new ArrayList();

	@RequestMapping("/ptdl")
	@ResponseBody
	public String ptdl(HttpServletResponse response, HttpSession session,HttpServletRequest request, String account, String password,
			String DXYZ) throws ClientProtocolException, IOException {
			SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		String interfaceUrl = ReadConfig.readValue();
		Map reqMap = new HashMap();
		Map reqMapm = new HashMap();
		Map remap = new HashMap();
		BODY body = new BODY();
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		String Key="";
		if(sessionIn!=null){
			if(!(sessionIn.getKEY().equals("")|| sessionIn.getKEY()==null)){
				 Key=sessionIn.getKEY();
			}
		}
		String a="";
		String Timestamp=sdf.format(date);
		String jm="123456"+Timestamp+"y1uUuBiikf0a5FHZ";
		// 接口编码
		System.out.println(jm);
		MD5 md5=new MD5();
		a=md5.getMD5(jm).toString();
		if(sessionIn==null){
			body.setPassWd(a);
				remap.put("AUTHORITY", "YBGJJ100");
				// 接口参数
				remap.put("LOGUserName", "admin");
				remap.put("BODY", body);
				body.setTimestamp(Timestamp);
				String json3 = JSONUtil.ToJsongString2(remap);
				// 请求
				String result2 = HttpRequest.doPost(interfaceUrl, json3);
				logger.info("接口返回内容:" + result2);
				Map retMap2 = JSONUtil.toMap(result2);
				try {
					if(retMap2.get("RETURNCODE").equals("100")){
						Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
						List jx = (List) mx2.get("RETDATA");
						Map m = (Map) jx.get(0);
						Key += m.get("OpenKey").toString();		
					}else{
						map.put("code", 1);
						map.put("CW", retMap2.get("RETURNDESC").toString());
						list.add(retMap2);
						response.setHeader("Content-type", "text/html;charset=UTF-8");
						response.setCharacterEncoding("UTF-8");
						String jsonString = JSONUtil.ToJsongString2(map);
						PrintWriter pw = response.getWriter();
						pw.write(jsonString);
						return null;
					}
				} catch (Exception e) {
					map.put("code", 1);
					map.put("CW", "登录失败！");
					list.add(retMap2);
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
					return null;
				}
				
				
		}else if(sessionIn.getKEY()==null){
			body.setPassWd(a);
			remap.put("AUTHORITY", "YBGJJ100");
			// 接口参数
			remap.put("LOGUserName", "admin");
			remap.put("BODY", body);
			String json3 = JSONUtil.ToJsongString2(remap);
			// 请求
			String result2 = HttpRequest.doPost(interfaceUrl, json3);
			logger.info("接口返回内容:" + result2);
			Map retMap2 = JSONUtil.toMap(result2);
			if(retMap2.get("RETURNCODE").equals("100")){
				Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
				List jx = (List) mx2.get("RETDATA");
				Map m = (Map) jx.get(0);
				Key +=m.get("OpenKey").toString();		
			}else{
				map.put("code", 1);
				map.put("CW", retMap2.get("RETURNDESC").toString());
				list.add(retMap2);
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
				return null;
			}
			
		}
		// 接口编码
		reqMapm.put("AUTHORITY", "YBGJJ101");
		// 接口参数
		BODY y =new BODY();
		reqMapm.put("LOGUserName", "admin");
		y.setPassWord(password);
		y.setUserName(account);
		reqMapm.put("BODY",y);
		if(Key.equals("")||Key==null){
			reqMapm.put("OpenKey",sessionIn.getKEY());
		}else{
			reqMapm.put("OpenKey",Key);
		}
		String result=OpenKey.getOpenKey(reqMapm, session);
		logger.info("接口返回内容:" + result);
		if (result != null) {
			Map resMap = JSONUtil.toMap(result);
			if (resMap.get("RETURNCODE").equals("100")) {
				Map retMap2 = new HashMap();
				retMap2.put("AUTHORITY", "YBGJJ106");
				BODY by=new BODY();
				by.setZJHM(account);
				retMap2.put("BODY", by);
				if(Key.equals("")||Key==null){
					retMap2.put("OpenKey",sessionIn.getKEY());
				}else{
					retMap2.put("OpenKey",Key);
				}
				retMap2.put("LOGUserName","admin");
				String retMap3=OpenKey.getOpenKey(retMap2, session);
				Map resMap1 = JSONUtil.toMap(retMap3);
				logger.info("接口返回内容:" + resMap1);
				if (resMap1.get("RETURNCODE").equals("100")) {
					
					Map mx = JSONUtil.toMap(JSONUtil.ToJsongString2(resMap1.get("body")));
					List l = (List) mx.get("RETDATA");
					JSONArray arr = new JSONArray(l);
					if (arr.size() > 0 && arr != null) {
						Map i = (Map) arr.get(0);
						SessionInfo sessionInfo = new SessionInfo();
						sessionInfo.setXINGMING(i.get("XINGMING").toString());
						sessionInfo.setDWZH(i.get("DWZH").toString());
						sessionInfo.setGRZH(i.get("GRZH").toString());
						sessionInfo.setSJHM(i.get("SJHM").toString());
						sessionInfo.setZJHM(i.get("ZJHM").toString());
						sessionInfo.setDWMC(i.get("DWMC").toString());
						sessionInfo.setGRZHZT(i.get("GRZHZT").toString());
						sessionInfo.setIp(IpUtil.getIpAddr(request));
						sessionInfo.setKEY(Key);
						session.setAttribute(ConfigUtil.getSessionInfoName(),sessionInfo);
						map.put("code", 0);
						map.put("msg", null);
						map.put("data", resMap.get("body"));
						list.add(resMap);
						response.setHeader("Content-type","text/html;charset=UTF-8");
						response.setCharacterEncoding("UTF-8");
						String jsonString = JSONUtil.ToJsongString2(map);
						PrintWriter pw = response.getWriter();
						pw.write(jsonString);
					} 
				}
				else {
					map.put("code", 1);
					map.put("CW", resMap1.get("RETURNDESC").toString());
					list.add(resMap1);
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
				}
			} else {
				map.put("code", 1);
				map.put("CW", resMap.get("RETURNDESC").toString());
				list.add(resMap);
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}
		}
		return null;
	}

	@RequestMapping("/gjdl")
	@ResponseBody
	public String gjdl(HttpServletResponse response, HttpSession session,
			HttpServletRequest request, String account, String password,
			String DXYZ) throws ClientProtocolException, IOException {
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		String interfaceUrl = ReadConfig.readValue();
		Map reqMap = new HashMap();
		Map reqMapm = new HashMap();
		Map remap = new HashMap();
		BODY body = new BODY();
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		String Key="";
		String a="";
		String Timestamp=sdf.format(date);
		String jm="123456"+Timestamp+"y1uUuBiikf0a5FHZ";
		// 接口编码
		System.out.println(jm);
		MD5 md5=new MD5();
		a=md5.getMD5(jm).toString();
		// 接口编码
		if(sessionIn==null){
				body.setPassWd(a);
				body.setTimestamp(Timestamp);
				remap.put("AUTHORITY", "YBGJJ100");
				// 接口参数
				remap.put("LOGUserName", "admin");
				remap.put("BODY", body);
				String json3 = JSONUtil.ToJsongString2(remap);
				// 请求
				String result2 = HttpRequest.doPost(interfaceUrl, json3);
				logger.info("接口返回内容:" + result2);
				Map retMap2 = JSONUtil.toMap(result2);
				Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
				List jx = (List) mx2.get("RETDATA");
				Map m = (Map) jx.get(0);
				Key += m.get("OpenKey").toString();		
				
		}else if(sessionIn.getKEY()==null){
			body.setPassWd("123456");
			remap.put("AUTHORITY", "YBGJJ100");
			// 接口参数
			remap.put("LOGUserName", "admin");
			remap.put("BODY", body);
			String json3 = JSONUtil.ToJsongString2(remap);
			// 请求
			String result2 = HttpRequest.doPost(interfaceUrl, json3);
			logger.info("接口返回内容:" + result2);
			Map retMap2 = JSONUtil.toMap(result2);
			Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
			List jx = (List) mx2.get("RETDATA");
			Map m = (Map) jx.get(0);
			Key +=m.get("OpenKey").toString();
			
		}
		// 接口编码
		reqMapm.put("AUTHORITY", "YBGJJ101");
		// 接口参数
		BODY y =new BODY();
		reqMapm.put("LOGUserName", "admin");
		y.setPassWord(password);
		y.setUserName(account);
		reqMapm.put("BODY",y);
		if(Key==""){
			reqMapm.put("OpenKey",sessionIn.getKEY());
		}else if(sessionIn==null){
			reqMapm.put("OpenKey",Key);
		}
		String result=OpenKey.getOpenKey(reqMapm, session);
		logger.info("接口返回内容:" + result);
		if (result != null) {
			Map resMap = JSONUtil.toMap(result);
			if (resMap.get("RETURNCODE").equals("100")) {
				Map retMap2 = new HashMap();
				retMap2.put("AUTHORITY", "YBGJJ106");
				BODY by=new BODY();
				by.setZJHM(account);
				retMap2.put("BODY", by);
				if(Key.equals("")||Key==null){
					retMap2.put("OpenKey",sessionIn.getKEY());
				}else{
					retMap2.put("OpenKey",Key);
				}
				retMap2.put("LOGUserName","admin");
				String retMap3=OpenKey.getOpenKey(retMap2, session);
				Map resMap1 = JSONUtil.toMap(retMap3);
				logger.info("接口返回内容:" + resMap1);
				if (resMap1.get("RETURNCODE").equals("100")) {
					
					Map mx = JSONUtil.toMap(JSONUtil.ToJsongString2(resMap1.get("body")));
					List l = (List) mx.get("RETDATA");
					JSONArray arr = new JSONArray(l);
					if (arr.size() > 0 && arr != null) {
						Map i = (Map) arr.get(0);
						SessionInfo sessionInfo = new SessionInfo();
						sessionInfo.setXINGMING(i.get("XINGMING").toString());
						sessionInfo.setDWZH(i.get("DWZH").toString());
						sessionInfo.setGRZH(i.get("GRZH").toString());
						sessionInfo.setSJHM(i.get("SJHM").toString());
						sessionInfo.setZJHM(i.get("ZJHM").toString());
						sessionInfo.setDWMC(i.get("DWMC").toString());
						sessionInfo.setGRZHZT(i.get("GRZHZT").toString());
						sessionInfo.setIp(IpUtil.getIpAddr(request));
						sessionInfo.setKEY(Key);
						session.setAttribute(ConfigUtil.getSessionInfoName(),sessionInfo);
						map.put("code", 0);
						map.put("msg", null);
						map.put("data", resMap.get("body"));
						list.add(resMap);
						response.setHeader("Content-type","text/html;charset=UTF-8");
						response.setCharacterEncoding("UTF-8");
						String jsonString = JSONUtil.ToJsongString2(map);
						PrintWriter pw = response.getWriter();
						pw.write(jsonString);
					} else {

					}
				} else {
					map.put("code", 1);
					list.add(resMap);
					response.setHeader("Content-type",
							"text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
				}
			} else {
				map.put("code", 1);
				list.add(resMap);
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}
		}
		return null;
	}

	@RequestMapping("/dwdl")
	@ResponseBody
	public String dwdl(HttpServletResponse response, HttpSession session,HttpServletRequest request, String account, String password,String DXYZ) throws ClientProtocolException, IOException {
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		String interfaceUrl = ReadConfig.readValue();
		Map reqMap = new HashMap();
		Map remap = new HashMap();
		BODY body=new BODY();
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		String Key="";
		if(sessionIn!=null){
			if(!(sessionIn.getKEY().equals("")|| sessionIn.getKEY()==null)){
				 Key=sessionIn.getKEY();
			}
		}
		String a="";
		String Timestamp=sdf.format(date);
		String jm="1234567"+Timestamp+"y1uUuBiikf0a5FHZ";
		// 接口编码
		System.out.println(jm);
		MD5 md5=new MD5();
		a=md5.getMD5(jm).toString();
		// 接口编码
		if(sessionIn==null){
				body.setPassWd(a);
				body.setTimestamp(Timestamp);
				remap.put("AUTHORITY", "YBGJJ100");
				// 接口参数
				remap.put("LOGUserName", "admin1");
				remap.put("BODY", body);
				String json3 = JSONUtil.ToJsongString2(remap);
				// 请求
				String result2 = HttpRequest.doPost(interfaceUrl, json3);
				logger.info("接口返回内容:" + result2);
				Map retMap2 = JSONUtil.toMap(result2);
				if(retMap2.get("RETURNCODE").equals("100")){
					Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
					List jx = (List) mx2.get("RETDATA");
					Map m = (Map) jx.get(0);
					Key += m.get("OpenKey").toString();		
				}else{
					map.put("code", 1);
					map.put("CW", retMap2.get("RETURNDESC").toString());
					list.add(retMap2);
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
					return null;
				}
		}else if(sessionIn.getKEY()==null){
			body.setPassWd(a);
			remap.put("AUTHORITY", "YBGJJ100");
			// 接口参数
			remap.put("LOGUserName", "admin1");
			remap.put("BODY", body);
			String json3 = JSONUtil.ToJsongString2(remap);
			// 请求
			String result2 = HttpRequest.doPost(interfaceUrl, json3);
			logger.info("接口返回内容:" + result2);
			Map retMap2 = JSONUtil.toMap(result2);
			if(retMap2.get("RETURNCODE").equals("100")){
				Map mx2 = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap2.get("body")));
				List jx = (List) mx2.get("RETDATA");
				Map m = (Map) jx.get(0);
				Key +=m.get("OpenKey").toString();		
			}else{
				map.put("code", 1);
				map.put("CW", retMap2.get("RETURNDESC").toString());
				list.add(retMap2);
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
				return null;
			}
		}
		
		BODY y =new BODY();
		// 接口编码
		reqMap.put("AUTHORITY", "YBGJJ103");
		reqMap.put("LOGUserName", "admin1");
		y.setEPassWord(password);
		y.setEUserName(account);
		reqMap.put("BODY",y);
		if(Key.equals("")||Key==null){
			reqMap.put("OpenKey",sessionIn.getKEY());
		}else{
			reqMap.put("OpenKey",Key);
		}
		String resultr=OpenKey.getOpenKey(reqMap, session);
		logger.info("接口返回内容:" + resultr);
		if (resultr != null) {
			Map resMap = JSONUtil.toMap(resultr);
			if (resMap.get("RETURNCODE").equals("100")) {
				Map retMap = new HashMap();
				BODY by =new BODY();
				by.setDWZH(account);
				retMap.put("AUTHORITY", "YBGJJ124");
				retMap.put("LOGUserName", "admin1");
				if(Key.equals("")||Key==null){
					retMap.put("OpenKey",sessionIn.getKEY());
				}else{
					retMap.put("OpenKey",Key);
				}
				retMap.put("BODY", by);
				String resulta=OpenKey.getOpenKey(retMap, session);
				Map retMap1 = JSONUtil.toMap(resulta);
				logger.info("接口返回内容:" + retMap1);
				if(retMap1.get("RETURNCODE").equals("100")){
					Map mx = JSONUtil.toMap(JSONUtil.ToJsongString2(retMap1.get("body")));
					List l = (List) mx.get("RETDATA");

					JSONArray arr = new JSONArray(l);
					Map i = (Map) arr.get(0);

					SessionInfo sessionInfo = new SessionInfo();
					sessionInfo.setDWZH(account);
					sessionInfo.setKEY(Key);	
					sessionInfo.setRQJCQK(i.get("JCQK").toString());
					sessionInfo.setDWMC(i.get("DWMC").toString());
					sessionInfo.setDQM(i.get("DQDM").toString());
					sessionInfo.setIp(IpUtil.getIpAddr(request));
					session.setAttribute(ConfigUtil.getSessionInfoName(),sessionInfo);
					map.put("code", 0);
					map.put("msg", null);
					map.put("data", resMap.get("body"));
					list.add(resMap);

					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
				}else{
					map.put("code", 1);
					map.put("CW", retMap1.get("RETURNDESC").toString());
					list.add(resMap);
					response.setHeader("Content-type", "text/html;charset=UTF-8");
					response.setCharacterEncoding("UTF-8");
					String jsonString = JSONUtil.ToJsongString2(map);
					PrintWriter pw = response.getWriter();
					pw.write(jsonString);
				}
			} else {
				map.put("code", 1);
				map.put("CW", resMap.get("RETURNDESC").toString());
				list.add(resMap);
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}
		}
		return null;
	}
}